///////////////////////////////////////////////////////////////////////////////
//
//                          IMPORTANT NOTICE
//
// The following open source license statement does not apply to any
// entity in the Exception List published by FMSoft.
//
// For more information, please visit:
//
// https://www.fmsoft.cn/exception-list
//
//////////////////////////////////////////////////////////////////////////////
/*
** This file is part of mg-tools, a collection of programs to convert
** and maintain the resource for MiniGUI.
**
** Copyright (C) 2010 ~ 2019, Beijing FMSoft Technologies Co., Ltd.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/gdi.h>

#include "dumpbmp.h"

extern BOOL RLE_ENCODE;

void encode_rle (FILE* fp, BYTE* bits, int width, int bytesPerPixel)
{
    int i, count = 0, run;

    while (count < width) {
        run = 1;
        while (((count ++) < width) && (run < 255) && memcmp (bits, bits + bytesPerPixel * run, bytesPerPixel) == 0)
            run ++;

        if (bytesPerPixel < 4)
            fprintf (fp, "0x%02x, ", (BYTE)run);
        else {
            fprintf (fp, "0x%02x, ", (BYTE)run);
            fprintf (fp, "0x%02x, ", 0);
        }

        for (i = 0; i < bytesPerPixel; i++)
            fprintf (fp, "0x%02x, ", bits [i]);

        fprintf (fp, "\n\t\t");

        bits += bytesPerPixel * run;
    }

    if (bytesPerPixel < 4)
        fprintf (fp, "0x00, ");
    else {
        fprintf (fp, "0x00, ");
        fprintf (fp, "0x00, ");
    }
}

int dump_bitmap (BITMAP* bmp, const char* prefix, FILE *fp_b)
{
    int i, j;
    char file [MAX_PATH + 1];
    FILE* fp;
    Uint8* buf;

    strcpy (file, prefix);
    strcat (file, ".c");

    if ((fp = fopen (file, "w+")) == NULL)
        return -1;

    fprintf (fp, "/* This file is generated by 'dumpbmp', do not edit manually. */\n\n");
    fprintf (fp, "#include <minigui/common.h>\n");
    fprintf (fp, "#include <minigui/gdi.h>\n");
    fprintf (fp, "\n");
    fprintf (fp, "static unsigned char %s_bits [] = {\n", prefix);

    buf = bmp->bmBits;
    for (i = 0; i < bmp->bmHeight; i ++) {
        fprintf (fp, "    ");
        if (RLE_ENCODE) {
            encode_rle (fp, buf, bmp->bmWidth, bmp->bmBytesPerPixel);
            buf += bmp->bmPitch;
        } else {
            for (j = 0; j < bmp->bmPitch; j ++) {
                fprintf (fp, "0x%02x, ", *buf);
                buf++;

                if (j != 0 && (j % 10 == 0))
                    fprintf (fp, "\n        ");
            }
        }
        fprintf (fp, "\n");
    }

    fprintf (fp, "};\n\n\n");

    /* make bitmap structure */

    if (RLE_ENCODE)
        bmp->bmType |= BMP_TYPE_RLE;
    if (!fp_b) {
        fprintf (fp, "static BITMAP %s_bmp = {\n", prefix);
        fprintf (fp, "    0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", 
                    bmp->bmType, bmp->bmBitsPerPixel, bmp->bmBytesPerPixel, bmp->bmAlpha);
        fprintf (fp, "    0x%08x,\n",
                    bmp->bmColorKey);
#ifdef _FOR_MONOBITMAP
        fprintf (fp, "    0x%08x,\n",
                    bmp->bmColorRep);
#endif
        fprintf (fp, "    0x%08x, 0x%08x, 0x%08x,\n",
                    bmp->bmWidth, bmp->bmHeight, bmp->bmPitch);
        fprintf (fp, "    %s_bits\n", prefix);
        fprintf (fp, "};\n\n");
    }
    else {
        fprintf (fp_b, "{\n");
        fprintf (fp_b, "    0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", 
                    bmp->bmType, bmp->bmBitsPerPixel, bmp->bmBytesPerPixel, bmp->bmAlpha);
        fprintf (fp_b, "    0x%08x,\n",
                    bmp->bmColorKey);
#ifdef _FOR_MONOBITMAP
        fprintf (fp_b, "    0x%08x,\n",
                    bmp->bmColorRep);
#endif
        fprintf (fp_b, "    0x%08x, 0x%08x, 0x%08x,\n",
                    bmp->bmWidth, bmp->bmHeight, bmp->bmPitch);
        fprintf (fp_b, "    %s_bits\n", prefix);
        fprintf (fp_b, "},\n\n");
    }

    fclose (fp);
    return 0;
}

